package cn.yanghan.service.impl;

import cn.yanghan.client.AuthServiceClient;
import cn.yanghan.domain.LoginUser;
import cn.yanghan.domain.VipBase;
import cn.yanghan.domain.VipUser;
import cn.yanghan.dto.LoginUserDto;
import cn.yanghan.dto.SMSSendLogDto;
import cn.yanghan.dto.UserRegisterDto;
import cn.yanghan.mapper.VipUserMapper;
import cn.yanghan.service.IVipBaseService;
import cn.yanghan.service.IVipUserService;
import cn.yanghan.utils.exception.Assert;
import cn.yanghan.utils.exception.BusinessException;
import cn.yanghan.utils.util.AjaxResult;
import cn.yanghan.utils.util.BitStatesUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author yh
 * @since 2022-04-13
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AuthServiceClient authServiceClient;

    @Autowired
    private IVipBaseService vipBaseService;
    /**
     * 用户注册
     * @param dto 封装的参数对象
     */
    @Override
    @GlobalTransactional
    public void register(UserRegisterDto dto) {
        //1.参数校验
        Assert.assertPhone(dto.getMobile(),"无效的手机号");
        //2.条件校验(手机验证码得对，手机号去重)
        //
        String smsCodeKey = "sms:"+dto.getMobile();
        SMSSendLogDto smsSendLogDto = (SMSSendLogDto) redisTemplate.opsForValue().get(smsCodeKey);
        Assert.assertNotNull(smsSendLogDto,"验证码过期或者无效");
        if(!smsSendLogDto.getCode().equals(dto.getSmsCode())){
            throw new BusinessException("验证码有误，请重新输入！！");
        }
        //手机号查重
        Wrapper<VipUser> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",dto.getMobile());
        VipUser vipUserFromDB = selectOne(wrapper);
        Assert.assertNull(vipUserFromDB,"手机号已经被注册");

        //3.保存login，返回login的id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(dto.getMobile());
        loginUser.setPassword(dto.getPassword());
        loginUser.setType(LoginUser.USER_TYPE_WEB);

        //* 3.1.用户中心集成Fegin
        //* 3.2.注入认证中心的Fegin，保存Login
        AjaxResult ajaxResult = authServiceClient.add(new LoginUserDto(loginUser, null));
        Assert.assertTrue(ajaxResult.isSuccess(),"登录信息保存失败");
        Assert.assertNotNull(ajaxResult.getResultObj(),"登录信息保存失败");
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        //* 4.保存vip_user,设置loginid
        VipUser vipUser = new VipUser();
        vipUser.setCreateTime(new Date().getTime());
        vipUser.setPhone(dto.getMobile());
        vipUser.setNickName(dto.getMobile());
        vipUser.setLoginId(loginId);

        //注册成功
        long bitState = BitStatesUtils.addState(0l, BitStatesUtils.OP_REGISTED);
        //账号激活
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_ACTIVED);
        //手机认证
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_AUTHED_PHONE);
        vipUser.setBitState(bitState);
        insert(vipUser);

        //* 5.保存vip_base
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(vipUser.getCreateTime());
        vipBase.setSsoId(vipUser.getId());
        //注册渠道
        vipBase.setRegChannel(dto.getRegChannel());
        vipBase.setRegTime(vipUser.getCreateTime());
        vipBaseService.insert(vipBase);

    }
}
